package com.aiju.forexpand.addresspicker;

import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.aiju.base.R;
import com.aiju.forexpand.addresspicker.widget.OnWheelChangedListener;
import com.aiju.forexpand.addresspicker.widget.WheelView;
import com.aiju.forexpand.addresspicker.widget.adapters.ArrayWheelAdapter;
import com.aiju.utils.StringUtil;

/**
 * 作者：流冥 on 2017/7/13 19:53
 * 说明：
 */

public class SinglePicker implements OnWheelChangedListener {

    /**
     * 当前选中 的名称
     */
    protected String mCurrentProviceName;

    private Dialog dialog_photo;
    private View view;
    private Button mBtnConfirm;
    private Button btn_cancle;
    private WheelView mViewProvince;
    private TextView address;
    private TextView titleTextView;
    String[] items;

    public SinglePicker() {
    }

    /**
     * 照片选择Dialog
     */
    public void selectAddressDialog(Context context, TextView address, int resId) {
        this.context = context;
        this.address = address;
        dialog_photo = new Dialog(context, R.style.MyDialogStyle);
        dialog_photo.requestWindowFeature(Window.FEATURE_NO_TITLE);
        LayoutInflater inflater = LayoutInflater.from(context);
        view = inflater.inflate(R.layout.picker_address, null);

        titleTextView = (TextView) view.findViewById(R.id.tv_title);
        Window window = dialog_photo.getWindow();
        // 重新设置
        WindowManager.LayoutParams lp = window.getAttributes();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.addresspickerstyle); // 添加动画
        window.setAttributes(lp);
        dialog_photo.setContentView(view);
          /*
         * 将对话框的大小按屏幕大小的百分比设置
         */
        WindowManager m = window.getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        WindowManager.LayoutParams p = window.getAttributes(); // 获取对话框当前的参数值
//        p.height = (int) (d.getHeight() * 0.4); // 高度设置为屏幕的0.6
        p.width = (int) (d.getWidth() * 1.0); // 宽度设置为屏幕的0.65
        window.setAttributes(p);
        lp.x = 0; // 新位置X坐标
        lp.y = 0; // 新位置Y坐标
        setUpViews();
        setUpListener();
        setUpData(resId);
        dialog_photo.setCanceledOnTouchOutside(true);
        dialog_photo.show();
    }


    private void setUpViews() {
        mViewProvince = (WheelView) view.findViewById(R.id.id_province);
        mViewCity = (WheelView) view.findViewById(R.id.id_city);
        mViewDistrict = (WheelView) view.findViewById(R.id.id_district);
        mBtnConfirm = (Button) view.findViewById(R.id.btn_confirm);
        btn_cancle = (Button) view.findViewById(R.id.btn_cancle);
        mViewCity.setVisibility(View.GONE);
        mViewDistrict.setVisibility(View.GONE);//隐藏县级市，只有省和市
    }

    private void setUpListener() {
        // 添加change事件
        mViewProvince.addChangingListener(this);
        // 添加change事件
//        mViewCity.addChangingListener(this);
//        // 添加change事件
//        mViewDistrict.addChangingListener(this);
        // 添加onclick事件
        mBtnConfirm.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                showSelectedResult();
            }
        });
        btn_cancle.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                dialog_photo.dismiss();
                dialog_photo = null;
            }
        });
    }


    private Context context;
    private WheelView mViewDistrict;
    private WheelView mViewCity;

    private void setUpData(int resId) {
        items = context.getResources().getStringArray(resId);
        String trim = address.getText().toString().trim();

        mViewProvince.setViewAdapter(new ArrayWheelAdapter<>(context, items));
        // 设置可见条目数量
        mViewProvince.setVisibleItems(5);
        if (!StringUtil.isBlank(trim)) {
            for (int i = 0; i < items.length; i++) {
                if (trim.equals(items[i])) {
                    mViewProvince.setCurrentItem(i);
                    break;
                }
            }
        }
    }


    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        // TODO Auto-generated method stub
    }


    private void showSelectedResult() {
        if (address != null)
            address.setText(items[mViewProvince.getCurrentItem()]);
        if (changeCity != null) {
            changeCity.onChangeCity(items[mViewProvince.getCurrentItem()]);
        }
        dialog_photo.dismiss();
        dialog_photo = null;
    }


    public void setChangeCity(OnChangeCity changeCity) {
        this.changeCity = changeCity;
    }

    private OnChangeCity changeCity;

    public interface OnChangeCity {
        void onChangeCity(String province);
    }

    public TextView getTitleTextView() {
        return titleTextView;
    }
}
